package com.wyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyz.mapper.InStorageMapper;
import com.wyz.model.InStorage;
import com.wyz.model.Supplier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/7/27 15:53
 */
@Controller
public class InStorageController {
    @Resource
    private InStorageMapper inStorageMapper;
    @RequestMapping("/inStorage/getAllInStorage")
    public String getAllInStorage(HttpSession session) {
        List<InStorage> inStorageList = inStorageMapper.selectList(null);
        session.setAttribute("instorages", inStorageList);
        return "back/in_storage";
    }
    @RequestMapping("/inStorage/getInStorageById")
    public String getInStorageById(Integer id,HttpSession session) {
        InStorage inStorage = inStorageMapper.selectById(id);
        session.setAttribute("inStorage", inStorage);
        return "back/in_storageView";
    }
    @RequestMapping("/inStorage/add.do")
    public String inStorageAdd(InStorage inStorage,HttpSession session) {
        inStorageMapper.insert(inStorage);
        return "redirect:/inStorage/getAllInStorage";
    }
    @RequestMapping("/inStorage/addInStorage")
    public String inStorageAdd() {
        return "back/in_storageAdd";
    }
    @RequestMapping("/inStorage/deleteInStorage")
    public String inStorageDel(Integer id) {
        inStorageMapper.deleteById(id);
        return "redirect:/inStorage/getAllInStorage";
    }
    @RequestMapping("/inStorage/edit.do")
    public String inStorageEdit(Integer id,HttpSession session) {
        InStorage inStorage = inStorageMapper.selectById(id);
        session.setAttribute("inStorage", inStorage);
        return "back/in_storageEdit";
    }
    @RequestMapping("/inStorage/editHandler.do")
    public String inStorageEditHandler(InStorage inStorage,HttpSession session) {
        int result =inStorageMapper.updateById(inStorage);
        if (result == 0) {
            session.setAttribute("error", "修改失败"); // 修改失败后将错误信息存入session中
            return "back/in_storageEdit";
        } else {
            session.removeAttribute("error"); // 修改成功后清除session中错误信息
            return "redirect:/inStorage/getAllInStorage"; // 修改成功后重定向跳转到登录页面
        }

    }
    @RequestMapping("/inStorage/search.do")
    public String search(String condition,String sort,HttpSession session) {
        QueryWrapper<InStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("instorage_id","%"+condition+"%")
                .or().like("goods_barcode","%"+condition+"%")
                .or().like("purchasingPerson","%"+condition+"%")
                .or().like("supplierCompany","%"+condition+"%")
                .or().like("goods_name","%"+condition+"%")
                .or().like("goods_specification","%"+condition+"%")
                .or().like("instorage_remark","%"+condition+"%")
                .orderByAsc(sort);
        List<InStorage> inStorages = inStorageMapper.selectList(queryWrapper);
        session.setAttribute("instorages",inStorages);
        return "back/in_storage";
    }

}
